//
//  DHomePageTwoViewController.m
//  B_WatchHome
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 DHF. All rights reserved.
//

#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)

#import "DHomePageTwoViewController.h"
#import "DHomePageViewController.h"
#import "DHomePageDetailViewController.h"
#import "HomePageTwoCollectionViewCell.h"
#import "HomePageModel.h"

@interface DHomePageTwoViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation DHomePageTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    //    设置每个item的大小
    flowLayout.itemSize = CGSizeMake((172.5*WIDTH)/375, (180*HEIGHT)/667);
    //    最小行间距
    flowLayout.minimumLineSpacing = (30*WIDTH)/375;
    
    //    最小列间距
    flowLayout.minimumInteritemSpacing =(2*HEIGHT)/667;
    
    //    设置item与四周边界的距离，上左下右
    flowLayout.sectionInset = UIEdgeInsetsMake((10*WIDTH)/375, (10*HEIGHT)/667, (10*WIDTH)/375, (10*HEIGHT)/667);
    
    //    设置滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView  *collectionView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flowLayout];
    
    [self.view addSubview:collectionView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    collectionView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:0.8];
    //隐藏滑动条
    [collectionView setShowsVerticalScrollIndicator:NO];
    //代理方法
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    //注册cell
    [collectionView registerClass:[HomePageTwoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}

//点左侧按钮 反回上一页
- (void)leftButtonAction:(UIBarButtonItem *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

//区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allArray.count;
}
//cell显示
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    HomePageTwoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    HomePageModel *model = [self.allArray objectAtIndex:indexPath.item];
    cell.model = model;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DHomePageDetailViewController *detailVC = [[DHomePageDetailViewController alloc] init];
    
    HomePageModel *model = [self.allArray objectAtIndex:indexPath.item];
    detailVC.model = model;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
